from urllib.parse import urlencode
import gloal.redis as redis
import requests
from gloal.config import sendInfoText

base_url = 'http://weixin.sogou.com/weixin?'
KEYWORD = 'python'

# 连接redis
r = redis.create_conn()
r_key = "checkerror:ai:url"
def get_html(url):
    print(url)
    try:
        response = requests.get(url)
        print(response)
        if response.status_code == 200:
            return response.text
    except ConnectionError:
        if not r.exists(r_key):
            text = "【Ai创业营】h5链接被封！请尽快处理~"
            sendInfoText(text)
            r.set("checkerror:ai:url", text, 1800)


def get_index(keyword, page):
    data = {
        'query': keyword,
        'type': 2,
        'page': page
    }
    queries = urlencode(data)
    url = base_url + queries
    html = get_html(url)
    print(html)


if __name__ == '__main__':
    # get_index(KEYWORD, 1)
    get_html("https://sdasi.kuaizhan.com/59/34/p106604037664be6")
